/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Review.test1;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.regex.*;

/**
 *
 * @author THIEN
 */
public class FanManagement {

    /**
     * create reader and array list
     */
    Fan fan;
    ArrayList<Fan> FanList = new ArrayList<Fan>();
    InputStreamReader reader = new InputStreamReader(System.in);
    BufferedReader in = new BufferedReader(reader);

    public void inputData() throws IOException {
        for (int i = 0; i < 2; i++) {
            System.out.println("Input fan's model: ");
            String model = in.readLine();

            System.out.println("Input fan's power: ");
            double power = Double.parseDouble(in.readLine());

            System.out.println("Is remove control (Y/N): ");
            boolean isRemoteControl = Boolean.parseBoolean(in.readLine());

            fan = new Fan(model, power, isRemoteControl);
            FanList.add(fan);
        }
    }

    public void displayData() {
        for (Fan fan : FanList) {
            System.out.println(fan);

        }
    }

    public void Save2file() throws FileNotFoundException, IOException {
        FileOutputStream f = new FileOutputStream("fan.dat", true);
        ObjectOutputStream writer = new ObjectOutputStream(f);
        writer.writeObject(FanList);
        writer.flush();
        writer.close();
        f.close();
    }

    public void readFromFile() throws FileNotFoundException, IOException {
        FileReader fReader = new FileReader("fan.dat");
        BufferedReader bReader = new BufferedReader(fReader);
        ArrayList<Fan> list = (ArrayList<Fan>) FanList;
        for (Fan fan : list) {
            System.out.println(fan);

        }
    }

    public void searchData() throws IOException {
        System.out.println("Input fan's model want to search: ");
        String searchObj = in.readLine();
        Pattern pattObj = Pattern.compile(searchObj, Pattern.CASE_INSENSITIVE);

        for (Fan fan : FanList) {
            String temp = fan.getModel();
            Matcher matObj = pattObj.matcher(temp);
            while (matObj.find()) {
                System.out.println("Found");
                System.out.println(fan);
            }
        }

    }
}
